package com.hhh.demo.sx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhh.demo.sx.bean.*;
import com.hhh.demo.sx.exception.MyException;
import com.hhh.demo.sx.service.ProductService;
import com.hhh.demo.sx.service.RecordService;
import com.hhh.demo.sx.service.StoreProductService;
import com.hhh.demo.sx.util.MD5Util;
import com.hhh.demo.sx.util.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hhh
 * @since 2022-01-03
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private RecordService recordService;
    @Autowired
    private StoreProductService storeProductService;
    /**
     * 打开商品主页面
     */
    @RequestMapping("/index")
    public String openPage() {
        return "product";
    }

    /**
     * 查询商品列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map<String, Object> getList(@RequestParam("page") int p,
                                       @RequestParam("limit") int limit,
                                       @RequestParam(value = "condition", required = false) String condition) {

        return productService.findAll(p, limit, condition);

    }

    /**
     * 新增/修改商品
     */
    @ResponseBody
    @RequestMapping("/saveOrUpdate")
    public ResultMap saveOrUpdate(Product product) {
        System.out.println(product);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("name", product.getName());
        if (productService.getOne(wrapper) != null&&product.getId()==null) {
            throw new MyException("该商品已存在");

        }
        boolean f = productService.saveOrUpdate(product);
        if (f) {
            return ResultMap.success();
        } else {
            return ResultMap.fail();
        }
    }

    /**
     * 查找商品
     */
    @ResponseBody
    @RequestMapping("/getOne")
    public ResultMap getOne(int id) {
        Product product = productService.getById(id);
        return ResultMap.success(product);
    }

    /**
     * 删除商品
     */
    @ResponseBody
    @RequestMapping("/delete")
    public ResultMap delete(int id) {
        boolean f = productService.removeById(id);
        if (f) {
            return ResultMap.success();
        } else {
            return ResultMap.fail();
        }
    }
    //通过仓库id查商品
    @RequestMapping("/findBySid")
    @ResponseBody
    public Map<String, Object> findBySId(Integer storeId){
        return productService.findBySid(storeId);
    }


    @ResponseBody
    @RequestMapping("/modify")
    public ResultMap modify(String sid,String pid,String type,String num, String toStoreId){
        System.out.println(sid);
        System.out.println(pid);
        System.out.println(type);
        System.out.println(num);
        System.out.println(toStoreId);


        Record record =new Record();
        record.setStoreId(Integer.valueOf(sid));
        record.setProductId(Integer.valueOf(pid));
        record.setType(Integer.valueOf(type));
        record.setNum(Integer.valueOf(num));
        if (!("").equals(toStoreId)){
            record.setToStoreId(Integer.valueOf(toStoreId));
        }



        System.out.println(record);
        recordService.addMemory(record);
        QueryWrapper<StoreProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id",record.getStoreId());
        wrapper.eq("product_id",record.getProductId());
        StoreProduct storeProduct = storeProductService.getOne(wrapper);
        if (record.getType()==0){//购入
            storeProduct.setNum(storeProduct.getNum()+record.getNum());
            storeProductService.saveOrUpdate(storeProduct);
        }else if(record.getType()==1){//销售
            storeProduct.setNum(storeProduct.getNum()-record.getNum());
            storeProductService.saveOrUpdate(storeProduct);
        }else {//调度
            wrapper.clear();
            storeProduct.setNum(storeProduct.getNum()-record.getNum());
            storeProductService.saveOrUpdate(storeProduct);
            wrapper.eq("store_id",record.getToStoreId());
            wrapper.eq("product_id",record.getProductId());
            StoreProduct s1=storeProductService.getOne(wrapper);
            if (s1!=null){
                s1.setNum(s1.getNum()+record.getNum());
                storeProductService.saveOrUpdate(s1);
            }else{
                storeProductService.saveOrUpdate(new StoreProduct(record.getToStoreId(),record.getProductId(),record.getNum()));
            }

        }

        return ResultMap.success();
    }

}

